package evil.wolf.panels;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;


/**
 * A pane with a tree with the contact persons in the different countries.
 * 
 * @author Heidi Rakels.
 */
@SuppressWarnings("serial")
public class PerformentTree extends JPanel
{

	/**
	 * Constructs a tree with the contact persons in the different countries.
	 *
	 */
	public PerformentTree()
	{
		// Create the nodes of the countries and their contact person nodes.
		DefaultMutableTreeNode nodeHangHoa = new DefaultMutableTreeNode("HÀNG HÓA");
		nodeHangHoa.add(new DefaultMutableTreeNode(""));
		nodeHangHoa.add(new DefaultMutableTreeNode("Danh mục hàng hóa"));
		
		DefaultMutableTreeNode nodeNguoidung = new DefaultMutableTreeNode("NGƯỜI DÙNG");
//		nodeNguoidung.setI
		nodeNguoidung.add(new DefaultMutableTreeNode("Thông tin người dùng"));
		nodeNguoidung.add(new DefaultMutableTreeNode("Đổi mật khẩu"));
		DefaultMutableTreeNode addUser = new DefaultMutableTreeNode("++");
		nodeNguoidung.add(addUser);
		
		DefaultMutableTreeNode nodeKhachhang = new DefaultMutableTreeNode("KHÁCH HÀNG");
		nodeKhachhang.add(new DefaultMutableTreeNode("Danh sách khách hàng"));
		nodeKhachhang.add(new DefaultMutableTreeNode("Thông tin khách hàng"));
		
		DefaultMutableTreeNode nodeNghiepvu = new DefaultMutableTreeNode("NGHIỆP VỤ");
		nodeNghiepvu.add(new DefaultMutableTreeNode("Thông tin tồn đầu"));
		nodeNghiepvu.add(new DefaultMutableTreeNode("Nhập hàng"));
		nodeNghiepvu.add(new DefaultMutableTreeNode("Xuất hàng"));
		nodeNghiepvu.add(new DefaultMutableTreeNode("Tồn cuối"));
		
		// Create the root and add the country nodes.
		DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Quản lý");
		rootNode.add(nodeNguoidung);
		rootNode.add(nodeKhachhang);
		rootNode.add(nodeHangHoa);
		rootNode.add(nodeNghiepvu);
		
		// Create the tree model.
		TreeModel treeModel = new DefaultTreeModel(rootNode);
		
		JTree tree = new JTree(treeModel);
		//----------------------------------------------------------------
		tree.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent mc) {
				super.mouseClicked(mc);

//				HangHoa hh = new HangHoa();
//				hh.setVisible(true);
			}
			
//			@Override
//			public void mousePressed(MouseEvent arg0) {
//				// TODO Auto-generated method stub
//				super.mousePressed(arg0);
//			}
		});
		//----------------------------------------------------------------
		tree.setRootVisible(false);
		// Expand the tree.
		for (int row = 0; row < tree.getRowCount(); row++)
		{
			tree.expandRow(row);
		}
		
		// Add the tree in a scroll pane.
		this.setLayout(new BorderLayout());
		JScrollPane jscroll = new JScrollPane(tree);
		this.add(jscroll, BorderLayout.CENTER);
		
		jscroll.setPreferredSize(new  Dimension(150, 300));
		tree.setPreferredSize(new  Dimension(150, 300));
		this.setPreferredSize(new Dimension(150, 300));
		
	}
	
	
}

@SuppressWarnings("serial")
class IconRenderer extends DefaultTreeCellRenderer {
    ImageIcon closedIcon;
    ImageIcon openIcon;
    ImageIcon leafIcon;
 
    public IconRenderer(BufferedImage[] images)
    {
        closedIcon = new ImageIcon(images[0]);
        openIcon   = new ImageIcon(images[1]);
        leafIcon   = new ImageIcon(images[2]);
    }
 
    public Component getTreeCellRendererComponent(JTree tree,
                                                  Object value,
                                                  boolean sel,
                                                  boolean expanded,
                                                  boolean leaf,
                                                  int row,
                                                  boolean hasFocus)
    {
        // start with default behavior
        super.getTreeCellRendererComponent(tree, value, sel, expanded, 
                                           leaf, row, hasFocus);
 
        // customize based on local conditions/state
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        String userObject = (String)node.getUserObject();
        boolean isOwl = userObject.equalsIgnoreCase("owls");
        if(isOwl)
            setIcon(closedIcon);
        else if(leaf)
            setIcon(leafIcon);
        else if(expanded)
            setIcon(openIcon);
        return this;
    }
}